do while loop in C

06-11-17 Course- C

To execute a portion of the program or code several times, we can use the C-language do while loop. As long as the condition is not correct, the code given between the blocks will be executed.

In the loop, the statement is given before the statement, so the statement or code lease will be executed once. In other words, we can say that it is executed 1 or more times.

do while loop syntax

The syntax of C language do-while loop is given below:


do{  
//code to be executed  
}while(condition);  

Flowchart of do while loop

 loop

DO WHILE IN C

do while example

There is given the simple program of c language do while loop where we are printing the table of 1.


#include <stdio.h>     
#include <conio.h>     
void main(){     
int i=1;   
clrscr();        
do{   
printf("%d \n",i);   
i++;   
}while(i<=10);        
getch();     
}    

Output


1
2
3
4
5
6
7
8
9
10

Program to print table for the given number using do while loop


#include <stdio.h>    
#include <conio.h>    
void main(){    
int i=1,number=0;  
clrscr();    
  
printf("Enter a number: ");  
scanf("%d",&number);  
  
do{  
printf("%d \n",(number*i));  
i++;  
}while(i<=10);  
    
getch();    
}    

Output


Enter a number: 5
5
10
15
20
25
30
35
40
45
50
Enter a number: 10
10
20
30
40
50
60
70
80
90
100

Infinitive do while loop

If you pass 1 as a expression in do while loop, it will run infinite number of times.


 
do{  
//statement  
}while(1);